#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Node {
	ll y, v;
	Node(ll _y, ll _v) {
		y = _y, v = _v;
	};
};
ll n, m, k, dist[100001], dist2[100001];
vector<Node> edge1[100001], edge2[100001];
set< pair<ll, ll>> q;
inline void dijkstra(vector<Node> edge[], ll s) {
	memset(dist, 127, sizeof(dist));
	dist[s] = 0;
	q.clear();
	for (ll i = 1; i <= n; ++i)
		q.insert({dist[i], i});
	for (; !q.empty(); ) {
		ll x = q.begin()->second;
		q.erase(q.begin());
		for (auto i : edge[x])
			if (dist[x] + i.v < dist[i.y]) {
				q.erase({dist[i.y], i.y});
				dist[i.y] = dist[x] + i.v;
				q.insert({dist[i.y], i.y});
			}
	}
}
 
int main() {
	scanf("%lld%lld%lld", &n, &m, &k);
	for (ll i = 1; i <= m; ++i) {
		ll x, y, z;
		scanf("%lld%lld%lld", &x, &y, &z);
		edge1[x].push_back(Node(y, z));
		edge2[y].push_back(Node(x, z));
	}
	dijkstra(edge2, k);
	memcpy(dist2, dist, sizeof(dist));
	dijkstra(edge1, k);
	ll ans = 0;
	for (ll i = 1; i <= n; ++i)
		ans = max(ans, dist[i] + dist2[i]);
	printf("%lld\n", ans);
	return 0;
}
